/*
 * LoginHandler.java
 * 
 * @version 1.0, March 10, 2011
 * CS 2340 Team 10: Cyberdyne Systems
 */
package edu.gatech.oad.cyberdoc.domain.action;

import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import edu.gatech.oad.cyberdoc.domain.User;

/**
 * Handles logins.
 * 
 * @author John Girata
 * @version 1.0, March 10, 2011
 */
public class LoginHandler extends ActionHandler {
	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	/**
	 * The action code this ActionHandler corresponds to.
	 */
	public static final String ACTION_CODE;

	static {
		ACTION_CODE = "login";
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.domain.action."
				+ "LoginHandler");
	}

	/**
	 * Registers this ActionHandler with the Patient class to start receiving
	 * actions.
	 * 
	 * This class requires an array storing all of the possible time slots that
	 * an appointment may be registered.
	 */
	public LoginHandler() {
		User.addActionHandler(ACTION_CODE, this);
	}
	
	/**
	 * Reverses everything done by the constructor.
	 */
	@Override
	public void destroy() {
		User.removeActionHandler(this);
	}

	/**
	 * Retrieves the list of available appointments for a given day.
	 * 
	 * This method expects the following fields:
	 * 
	 * username: The username of the user attempting to login.
	 * 
	 * password: The password of the user attempting to login.
	 * 
	 * This method will return the following fields:
	 * 
	 * action: The action code that was specified in the request
	 * 
	 * next: The next frame to show the user.
	 * 
	 * reason: The reason why the login failed, if applicable; one of
	 * "username", "password", or "suspended".
	 * 
	 * error_message: If applicable, a human-readable message explaining why the
	 * login failed, appriopriate for display to the user.
	 * 
	 * @param source
	 *            The user that called this method
	 * @param command
	 *            The command encoding the action
	 * @return The result of performing this action
	 * @throws ActionHandleException
	 *             If a required field does not exist in the command, or if the
	 *             usernames given for the patient and doctor are invalid
	 */
	@Override
	public JSONObject handle(User source, JSONObject command)
			throws ActionHandleException {
		final JSONObject response = new JSONObject();

		try {
			final String username = command.getString("username");
			final String password = command.getString("password");

			switch (source.checkLogin(username, password)) {
			case GOOD:
				response.put("next", "options");

				break;

			case BAD_NAME:
				LOG.severe("User.checkLogin() returned \"bad username\" "
						+ "unexpectedly for \"" + username + "\" on \""
						+ source + "\"");
				response.put("next", "login");
				response.put("reason", "username");
				response.put("error_message", "This username does not exist.");

				break;

			case BAD_PASS:
				response.put("next", "login");
				response.put("reason", "password");
				response.put("error_message", "Incorrect password.");

				break;

			case SUSPENDED:
				response.put("next", "login");
				response.put("reason", "suspended");
				response.put("error_message", "This account is suspended.");

				break;

			default:
				LOG.severe("User.checkLogin returned an unexpected result.");
			}
		} catch (JSONException e) {
			throw new ActionHandleException(e);
		}

		return response;
	}

	/**
	 * Returns a string representation of this class.
	 * 
	 * @return A string representation of this class.
	 */
	@Override
	public String toString() {
		return "LoginHandler (action=\"" + ACTION_CODE + "\")";
	}
}
